/*
 * JDateChooser.java
 *
 * Created on 7 de septiembre de 2008, 19:25
 */

package es.atareao.alejandria.gui;

import java.text.ParseException;
import java.util.Date;
import javax.swing.UIManager;
import javax.swing.event.ChangeListener;
import javax.swing.text.DefaultFormatterFactory;
import es.atareao.alejandria.lib.DateUtils;
import es.atareao.alejandria.lib.FormatoFecha;

/**
 *
 * @author  Propietario
 */
public class JDateChooser extends javax.swing.JPanel {
    //private final static long serialVersionUID=0L;
    /** Creates new form JDateChooser */
    public JDateChooser() {
        super();
        initComponents();
        inicia();
        JDateChooserDialog jds=new JDateChooserDialog(this);
        jds.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            @Override
                public void propertyChange(java.beans.PropertyChangeEvent evt) {
                    formPropertyChange(evt);
                }
        });
        this.setJds(jds);
        this.setDate(DateUtils.Ahora());
        try {
            this.jDate.setFormatterFactory(new DefaultFormatterFactory(new FormatoFecha(FormatoFecha.SHORT)));
        } catch (ParseException ex) {
            ErrorDialog.manejaError(ex);
        }
        this.jPopupMenu1.add(this.getJds());
    }
    private void formPropertyChange(java.beans.PropertyChangeEvent evt){
        if(evt.getPropertyName().equals("SelectedDateChanged")){
            Object newValue=evt.getNewValue();
            if(newValue instanceof Date){
                Date date=(Date)newValue;
                //this.getJds().setDate(date);
                this.jDate.setValue(date);
                //
                this.jPopupMenu1.setVisible(false);
                //
                this.firePropertyChange("DateChanged", evt.getOldValue(),evt.getNewValue());
            }
            
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jDate = new javax.swing.JFormattedTextField();
        jButton = new javax.swing.JButton();

        setMinimumSize(new java.awt.Dimension(200, 25));
        setOpaque(false);
        setPreferredSize(new java.awt.Dimension(200, 25));
        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.X_AXIS));

        jDate.setEditable(false);
        jDate.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jDate.setMinimumSize(new java.awt.Dimension(100, 25));
        jDate.setPreferredSize(new java.awt.Dimension(100, 25));
        add(jDate);

        jButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/es/atareao/alejandria/images/JCalendarColor16.gif"))); // NOI18N
        jButton.setFocusable(false);
        jButton.setMaximumSize(new java.awt.Dimension(30, 25));
        jButton.setMinimumSize(new java.awt.Dimension(30, 25));
        jButton.setPreferredSize(new java.awt.Dimension(30, 25));
        jButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonActionPerformed(evt);
            }
        });
        add(jButton);
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonActionPerformed
        this.saca();
}//GEN-LAST:event_jButtonActionPerformed
    private void saca(){
        this._jds.setDate((Date)this.jDate.getValue());
        int x = 0;
        int y = this.jDate.getY() + this.jDate.getHeight();
	this.jPopupMenu1.show(this.jDate, x, y);        
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton;
    private javax.swing.JFormattedTextField jDate;
    private javax.swing.JPopupMenu jPopupMenu1;
    // End of variables declaration//GEN-END:variables
    private JDateChooserDialog _jds;
    private ChangeListener _changeListener;
    private boolean _editable=true;
    private int _dateFormat;
    public Date getDate() {
        return (Date)this.jDate.getValue();
    }

    public void setDate(Date date) {
        this.getJds().setDate(date);
        this.jDate.setValue(date);
    }

    public JDateChooserDialog getJds() {
        return _jds;
    }

    public void setJds(JDateChooserDialog jds) {
        this._jds = jds;
    }

    public ChangeListener getChangeListener() {
        return _changeListener;
    }

    public void setChangeListener(ChangeListener changeListener) {
        this._changeListener = changeListener;
    }
    @Override
    public void setEnabled(boolean enabled){
        super.setEnabled(enabled);
        this.jDate.setEnabled(enabled);
        this.jButton.setEnabled(enabled);
    }
    @Override
    public boolean isEnabled(){
        return super.isEnabled();
    }
    public void setEditable(boolean editable){
        this._editable=editable;
        this.jDate.setEditable(editable);
        this.jButton.setEnabled(editable);
    }
    public boolean isEditable(){
        return this._editable;
    }
    public void inicia(){
        if(isEnabled()){
            if(isEditable()){
                this.jDate.setBackground(UIManager.getColor("TextField.background"));
                this.jButton.setEnabled(true);
            }else{
                this.jDate.setBackground(java.awt.SystemColor.info);
                this.jButton.setEnabled(false);
            }
        }else{
            this.jDate.setBackground(UIManager.getColor("TextField.disableBackground"));
            this.jButton.setEnabled(false);
        }        
    }

    public int getDateFormat() {
        return _dateFormat;
    }

    public void setDateFormat(int dateFormat) {
        try {
            this._dateFormat = dateFormat;
            this.jDate.setFormatterFactory(new DefaultFormatterFactory(new FormatoFecha(dateFormat)));
        } catch (ParseException ex) {
            
        }
    }
    /*
      * 
    @Override
    public void setBackground(Color color){
        super.setBackground(color);
        this.jDate.setBackground(color);
    }
    @Override
    public Color getBackground(){
        return super.getBackground();
    }*/
    //
}
